package com.chang.product.app;

import com.chang.common.utils.PageUtils;
import com.chang.common.utils.R;
import com.chang.common.validator.group.AddGroup;
import com.chang.common.validator.group.UpdateGroup;
import com.chang.common.validator.group.UpdateStatusGroup;
import com.chang.product.entity.BrandEntity;
import com.chang.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 品牌
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        System.out.println("params========>"+params);
        PageUtils page = brandService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    public R info(@PathVariable("brandId") Long brandId){
        BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@Validated(AddGroup.class) @RequestBody BrandEntity brand) {

        brandService.save(brand);
        return R.ok();
    }

    /*
     * @Author Chang
     * @Date 2024/3/6 9:43
     * @Description 品牌进行更新的时候关联表(CategoryBrandRelation)的属性也需要同步更新
     * @Param [brand]
     * @Return com.chang.common.utils.R
     * @Since version 1.0
     */
    @RequestMapping("/update")
    public R update(@Validated(UpdateGroup.class) @RequestBody BrandEntity brand) {
        brandService.updateDetail(brand);
        return R.ok();
    }

    // 修改已被逻辑删除的数据字段
    @RequestMapping("/updateDeleted")
    public R updateDeleteStatus(@Validated(UpdateStatusGroup.class) @RequestBody BrandEntity brand) {
        brandService.updateDeleted(brand);
        return R.ok();
    }

    @RequestMapping("/selectDeleted")
    public R updateDeleteStatus(@RequestParam Map<String, Object> params){
        System.out.println(params);
        PageUtils page =brandService.selectDeleted(params);
        return R.ok().put("page", page);
    }


    // 物理删除
    @RequestMapping("finalDelete")
    public R finalDelete(@RequestBody Long[] attrGroupIds) {
        brandService.finalDelete(Arrays.asList(attrGroupIds));
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] brandIds){
        brandService.removeByIds(Arrays.asList(brandIds));
        return R.ok();
    }

}
